package com.koron.sinoprobe.web.subpackage.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.bean.query.PageQuery;
import com.koron.bean.workflow.RedirectQuery;
import com.koron.bean.workflow.WorkflowController;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.sinoprobe.config.Constants;
import com.koron.sinoprobe.web.subpackage.bean.SubPackage;
import com.koron.sinoprobe.web.subpackage.bean.query.SubPackageQuery;
import com.koron.sinoprobe.web.subpackage.bean.vo.SubPackageVO;
import com.koron.sinoprobe.web.subpackage.service.SubPackageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/subPackage")
@Api(tags = "施工分包", description = "施工分包")
public class SubPackageController implements WorkflowController {

    @Autowired
    private SubPackageService subPackageService;


    public String getCurrDs(){
        return ThreadLocalContext.get().getCurrDs();
    }

    @PostMapping("/save")
    @ApiOperation("添加施工分包基本信息")
    public Response addSubPackage(@RequestBody SubPackage subPackage) {
        Assert.notNull(subPackage, "施工分包对象不能为空！");
        return Response.ok(ADOConnection.runTask(getCurrDs(), subPackageService,"saveOrUpdateSubPackage", SubPackage.class, subPackage));
    }

    @GetMapping("/getById")
    @ApiOperation("通过id获取施工分包基本信息")
    public Response getSubPackage(@RequestParam("id") String id) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), subPackageService, "getSubPackageById", SubPackageVO.class, id));
    }

    @PostMapping("/update")
    @ApiOperation("修改施工分包基本信息")
    public Response updateSubPackage( @RequestBody SubPackage subPackage) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), subPackageService, "updateSubPackage", SubPackage.class, subPackage));
    }

    @GetMapping("/{id}")
    @ApiOperation("删除施工分包基本信息")
    public Response deleteSubPackage(@PathVariable String id) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), subPackageService, "deleteSubPackage", Integer.class, id));
    }

    @GetMapping("/page")
    @ApiOperation("分页施工分包基本信息")
    public Response page(PageQuery pageQuery, SubPackageQuery subPackageQuery) {

        return Response.ok(ADOConnection.runTask(getCurrDs(), subPackageService, "page", PageInfo.class, pageQuery, subPackageQuery));
    }

    @Override
    public Response updateState(RedirectQuery redirectQuery) {
        log.info("工作流更新: {}", JSON.toJSONString(redirectQuery));
        // 撤回或者废弃
        String id = redirectQuery.getDataId();
        String status = null;
        if (redirectQuery.getOperation() == 1 || redirectQuery.getOperation() == 256) {
            // 修改为草稿状态
            status = Constants.SubPackageStatus.DRAFT;
        } else {
            // 修改为待审核状态
            status = Constants.SubPackageStatus.WAIT_REVIEW;
        }
        SubPackage subPackage = new SubPackage();
        subPackage.setId(id);
        subPackage.setStatus(status);

        ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), subPackageService, "updateSubPackage",  SubPackage.class, subPackage);
        return Response.success();
    }

    @Override
    public Response updateEnd(RedirectQuery redirectQuery) {
        String id = redirectQuery.getDataId();
        SubPackage subPackage = new SubPackage();
        subPackage.setId(id);
        subPackage.setStatus(Constants.SubPackageStatus.APPROVED);
        ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), subPackageService, "updateSubPackage",  SubPackage.class, subPackage);
        return Response.success();
    }

}
